import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules, ComponentSize } from 'element-plus';

// 获取当前日期  yyyy-MM-dd
export const getNowDate = () => {
    let date = new Date();
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}

// 将date-picker获取的日期格式成 yyyy-MM-dd
export const formatDate = (date: string) => {
    const d = new Date(date);
    let month = '' + (d.getMonth() + 1);
    let day = '' + d.getDate();
    const year = d.getFullYear();
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    return [year, month, day].join('-');
};

export const showSuccessMsg = (msg: string) => {
    ElMessage({
        showClose: true,
        message: msg,
        type: 'success',
    })
}
export const showErrorMsg = (msg: string) => {
    ElMessage({
        showClose: true,
        message: msg,
        type: 'error',
    })
}

export const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

export const indexMethod = (index: number) => {
    return index + 1
}

// return uuid
export const generateUUID = (): string => {
    if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
        console.warn("Your browser does not support crypto.getRandomValues()");
        return '';
    }

    const buffer = new Uint8Array(16);
    crypto.getRandomValues(buffer);

    // Set the version (4) and variant (8, 9, a, b)
    buffer[6] = (buffer[6] & 0x0f) | 0x40; // Version 4
    buffer[8] = (buffer[8] & 0x3f) | 0x80; // Variant is 10

    const hex = Array.from(buffer, byte => byte.toString(16).padStart(2, '0')).join('');

    return (
        hex.slice(0, 8) + '-' +
        hex.slice(8, 12) + '-' +
        hex.slice(12, 16) + '-' +
        hex.slice(16, 20) + '-' +
        hex.slice(20, 32)
    );
}

export interface ApiResponse<T> {
    status: number,
    data: T,
    msg: string,
}
export interface PageResponse<T> {
    total: string,
    records: T[]
}
export interface OnlyIdRequest {
    id: Number | null,
}
// 分页数据
export interface PageData<T> {
    // 总条数
    totalNum: number,
    tableData: T[]
}